'use strict';
exports.main = async (event, context) => {
  const db = uniCloud.database()
  
  try {
    // 获取当前用户ID
    const { OPENID, UNIONID, APPID } = context
    if (!OPENID) {
      return {
        code: 403,
        message: '未登录'
      }
    }
    
    // 查询用户设置
    const settings = await db.collection('user_settings')
      .where({
        user_id: OPENID
      })
      .get()
    
    // 如果没有设置记录，创建默认设置
    if (!settings.data.length) {
      const defaultSettings = {
        user_id: OPENID,
        systemNotification: true,
        activityNotification: true,
        careNotification: true,
        petInfoPublic: false,
        locationEnabled: true,
        createTime: Date.now()
      }
      
      await db.collection('user_settings').add(defaultSettings)
      return {
        code: 200,
        message: '获取成功',
        data: defaultSettings
      }
    }
    
    return {
      code: 200,
      message: '获取成功',
      data: settings.data[0]
    }
    
  } catch (error) {
    console.error('获取设置失败:', error)
    return {
      code: 500,
      message: error.message || '获取失败'
    }
  }
} 